package net.wanho.listener;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import net.wanho.po.Student;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

/**
 * Author：汤小洋
 * Date：2024-10-29 16:59
 * Description：<描述>
 */
@Component
public class MsgListener {


    @RabbitListener(queues = "${shop.queue}")
    public void readMsg(Student stu){
        System.out.println("消费者1号收到消息：" + stu);
    }

    @RabbitListener(bindings = @QueueBinding(
            value=@Queue("queue2"),
            exchange = @Exchange(value = "exchange2.fanout", type = ExchangeTypes.FANOUT)
    ))
    public void readMsg2(String msg){
        System.out.println("消费者2号收到消息：" + msg);
    }

    @SneakyThrows
    @RabbitListener(queues = "queue1")
    public void readMsg(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tags){
        System.out.println("消费者3号收到消息：" + msg);

        /**
         * 业务逻辑。。。。。
         */
        // 确认消息，处理消息成功
        // channel.basicAck(tags, false); // 参数：消息标签，是否批量确认

        // 不确认消息，处理消息失败
        // channel.basicNack(tags, false, false); // 参数：消息标签，是否批量确认， 是否重新入队
        channel.basicReject(tags, false);

        // 如果不确认消息，且不重新入队，该该消息称为死信

    }
}
